<?php
namespace App\Service\Admin;

class XcxService extends BaseService
{
    //小程序菜单

    public static function index()
    {
        $db = self::dbConn(self::$dbXcxMenu);
        $total = $db->count();
        $res = $db->orderBy('sort', 'desc')->get();
        if (!count($res)) {
            return self::toback(0, '', [], $total);
        }
        foreach ($res as &$v) {
            $v->icon0 = file_exists($v->icon0) ? $v->icon0 : '';
            $v->icon1 = file_exists($v->icon1) ? $v->icon1 : '';
        }
        return self::toback(0, '', $res, $total);
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbXcxMenu)->where('id', $id)->first();
        if ($res) {
            if ($pas['icon0'] && file_exists($res->icon0) && $pas['icon0'] != $res->icon0) {
                unlink($res->icon0);
            }
            if ($pas['icon1'] && file_exists($res->icon1) && $pas['icon1'] != $res->icon1) {
                unlink($res->icon1);
            }
        }
        self::dbConn(self::$dbXcxMenu)
            ->where('id', $id)
            ->update($pas);
        return self::toback();
    }

    /**
     * 配置获取
     */
    public static function enableOne()
    {
        $total = self::dbConn(self::$dbXcxMenu)
            ->whereIn('id', [2, 3])
            ->where('enable', 1)
            ->count();
        $res = $total==2 ? 1 : 0;
        return self::toback(0, '', $res);
    }

    /**
     * 配置设置
     */
    public static function setEnable()
    {
        $total = self::dbConn(self::$dbXcxMenu)
            ->whereIn('id', [2, 3])
            ->where('enable', 1)
            ->count();
        $enbale = $total==2 ? 0 : 1;
        self::dbConn(self::$dbXcxMenu)
            ->whereIn('id', [2, 3])
            ->update(['enable' => $enbale]);
        return self::toback();
    }
}